﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;
using System.Net.Cache;
using System.Text.RegularExpressions;

namespace TweetMyApps
{
    public class Duration : IEquatable<Duration>, IEquatable<int>, IComparable<Duration>
    {
        private static Regex parse = new Regex(@"^((\d*)d)?((\d*)h)?((\d*)m)?((\d*)s?)?$");

        private TimeSpan _duration;
        public Duration(TimeSpan ts)
        {
            _duration = ts;
        }

        public Duration(string text)
        {
            MatchCollection matches = parse.Matches(text.Trim());
            if (matches.Count > 0)
            {
                int h = 0;
                int d = 0;
                int m = 0;
                int s = 0;
                int.TryParse(matches[0].Groups[2].Value, out d);
                int.TryParse(matches[0].Groups[4].Value, out h);
                int.TryParse(matches[0].Groups[6].Value, out m);
                int.TryParse(matches[0].Groups[8].Value, out s);
                _duration = new TimeSpan(d, h, m, s);
            }
        }

        public Duration(int seconds)
        {
            _duration = new TimeSpan(0, 0, seconds);
        }

        public override string ToString()
        {
            return Utils.FormatTimeSpan(_duration, true);
        }

        public static implicit operator int(Duration duration)
        {
            return (int)duration._duration.TotalSeconds;
        }
        
        public bool Equals(Duration other)
        {
            if (other == null) return false;
            return this._duration == other._duration;
        }

        public bool Equals(int seconds)
        {
            return this._duration.TotalSeconds == seconds;
        }

        public int CompareTo(Duration other)
        {
            return _duration.TotalSeconds.CompareTo(other._duration.TotalSeconds);
        }
    }

    public class Utils
    {
        public static string FormatTimeSpan(TimeSpan ts)
        {
            return FormatTimeSpan(ts, false);
        }
        
        public static string FormatTimeSpan(TimeSpan ts, bool showSeconds)
        {
            string s = showSeconds ? (ts.Seconds == 0 ? "" : (ts.Seconds.ToString("00") + "s")) : "";
            if (ts.TotalDays >= 1)
            {
                return (int)ts.TotalDays + "d" + (ts.Hours == 0 ? "" : ts.Hours.ToString("00") + "h");
            }
            else if (ts.TotalHours >= 1)
            {
                return (int)ts.TotalHours + "h" + (ts.Minutes == 0 ? "" : (ts.Minutes.ToString("00") + "m")) + s;
            }
            else if (ts.TotalMinutes >= 1)
            {
                return (int)ts.TotalMinutes + "m" + s;
            }
            else
            {
                return (int)ts.TotalSeconds + "s";
            }
        }

        public static HttpWebRequest CreateHttpWebRequest(string url, string userAgent, IWebProxy proxy)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            RequestCachePolicy policy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
            request.CachePolicy = policy;
            if (!string.IsNullOrEmpty(userAgent))
                request.UserAgent = userAgent;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.UseDefaultCredentials = true;
            request.UnsafeAuthenticatedConnectionSharing = true;
            request.AutomaticDecompression = DecompressionMethods.GZip;
            if (proxy != null)
            {
                request.Proxy = proxy;
            }

            return request;
        }

        public static byte[] ConvertImageToByteArray(Image image, ImageFormat format)
        {
            byte[] res;

            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, format);
                res = ms.ToArray();
            }

            return res;
        }
    }
}
